package com.company.cloud.goodsrestservice.service.impl;

import com.company.cloud.goodsrestservice.core.AbstractService;
import com.company.cloud.goodsrestservice.dao.StockMapper;
import com.company.cloud.goodsrestservice.model.Stock;
import com.company.cloud.goodsrestservice.service.StockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * Created by reabout on 2018/03/24.
 */
@Service
@Transactional
public class StockServiceImpl extends AbstractService<Stock> implements StockService {
    @Resource
    private StockMapper skuStockMapper;

    private final Logger log = LoggerFactory.getLogger(StockService.class);

    public Stock chkInsert(Integer skuId, String stockLocation, Integer locationId, String caseNumber, Integer stockNumber) {
        Stock stock = new Stock();
        stock.setSkuId(skuId);
//        stock.setStockLocation(stockLocation);
        stock.setStockLocationId(locationId);
        stock = skuStockMapper.selectOne(stock);
        if (stock == null & stockLocation != "") {
            log.info("New stock {}", stockLocation);
            stock = new Stock();
            stock.setSkuId(skuId);
            stock.setStockCaseNumber(caseNumber);
            stock.setStockLocation(stockLocation);
            stock.setStockLocationId(locationId);
            stock.setStockNumber(stockNumber);
            save(stock);
            stock = skuStockMapper.selectOne(stock);
        } else {
            if (caseNumber != "") {
                stock.setStockCaseNumber(caseNumber);
            }
            if (stockNumber != null) {
                stock.setStockNumber(stockNumber);
            }
            update(stock);
        }
        return stock;
    }
}
